<?php
/**
 * Created by PhpStorm.
 * User: Admin
 * Date: 2020-08-01
 * Time: 下午 08:38
 */
namespace Action\Page;

use Action\Common\BaseLayout;
use Action\Common\Template;
use Action\Login;
use Model\Page;

/**
 * Class Edit
 * @property-read \Model\Page $pageModel
 * @package Action\Page
 */
class View extends Template {
    public function main($id=null) {
        $id = $id ? $id : $_GET['id'];
        
        if(empty($id)) {
            $this->notfound();
            die;
        }
        
        if($_GET['preview'] && !$_SESSION['logined']) {
            $this->core->redirectToAction('login');
            die;
        }
        
        $data = $this->pageModel->getInfo($id);
        
        if(!$data['enabled']) {
            if(!($_GET['preview'] && $_SESSION['logined'])) {
                $this->view('404');
                die;
            }
        }   
        
        $template = $data['template'];
        
        if(empty($template)) {
            $this->view('555');
        }
        
        $templatePath = $this->pageModel->getTemplatePath($template);
        
        if(!file_exists($templatePath)) {
            $this->view('555');
        }
        
        $templateData = $data;
        $data = $data['data'];
        
        if($_REQUEST['debug']) {
            echo "<div data='templateData' style='display: none;'><xmp>";
            print_r($templateData);
            echo "</xmp></div>";
        }
        
        include $templatePath;
    }
} 